package com.tianyu.service.init;

import com.alibaba.fastjson2.JSONObject;
import com.tianyu.common.AppConstants;
import com.tianyu.model.entity.Device;
import com.tianyu.model.entity.ThirdCompanyAccessChannel;
import com.tianyu.service.base.DeviceService;
import com.tianyu.service.base.ThirdCompanyAccessChannelService;
import com.tianyu.service.cache.DataCacheService;
import com.tianyu.service.tcp.TcpClient;
import com.tianyu.service.tcp.TianyuCompanyMessage;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.Executor;

import static com.tianyu.service.cache.DataCacheService.*;

/**********************************
 * @Author YSW
 * @Description 应用初始化
 * @Date 2024/7/15 - 14:31
 **********************************/

@Slf4j
@Component
public class InitComponent implements CommandLineRunner {

    @Resource
    private ThirdCompanyAccessChannelService thirdCompanyAccessChannelService;

    @Resource
    private DeviceService deviceService;

    @Resource
    private DataCacheService dataCacheService;

    @Resource
    private AppConstants appConstants;

    @Resource
    private Executor threadPool;

    @Override
    public void run(String... args) throws Exception {
        final List<ThirdCompanyAccessChannel> list = thirdCompanyAccessChannelService.getList();
        for (ThirdCompanyAccessChannel tcac : list) {
            DataCacheService.THIRD_ID_TO_TY_MAP.put(tcac.getThirdId(), tcac);
            DataCacheService.THIRD_ID_TO_TY_ID_MAP.put(tcac.getThirdId(), tcac.getYkCompanyId());
            DataCacheService.TY_ID_TO_THIRD_ID_MAP.put(tcac.getYkCompanyId(), tcac.getThirdId());
            final Integer ykCompanyId = tcac.getYkCompanyId();
            final JSONObject accessChannel = tcac.getAccessChannel();
            if (accessChannel != null) {
                tianyuThirdAccessChannelMap.put(ykCompanyId, accessChannel);
                final String token = accessChannel.getString("cpToken");
                tianyuLoginMap.put(ykCompanyId, TianyuCompanyMessage.builder().companyId(ykCompanyId).token(token).build());
            }
        }
        final List<Device> deviceList = deviceService.getList();
        for (Device device : deviceList) {
            final String sn = device.getSn();
            final Integer ykCompanyId = device.getYkCompanyId();
            final String companyCode = DataCacheService.TY_ID_TO_THIRD_ID_MAP.get(ykCompanyId);
            deviceCompanyCodeMap.put(sn, companyCode);
            deviceCompanyMap.put(sn, ykCompanyId);
        }
        // initTianyuTcpClient();
    }

    private void initTianyuTcpClient() {
        for (ThirdCompanyAccessChannel companyAccessChannel : THIRD_ID_TO_TY_MAP.values()) {
            final JSONObject accessChannel = companyAccessChannel.getAccessChannel();
            final String cpToken = accessChannel.getString("cpToken");
            final String cpAccessKey = accessChannel.getString("cpAccessKey");
            final String cpAccessSecret = accessChannel.getString("cpAccessSecret");
            if ((cpAccessKey != null && cpAccessSecret != null) || cpToken != null) {
                threadPool.execute(() -> new TcpClient(TianyuCompanyMessage.builder()
                        .companyId(companyAccessChannel.getYkCompanyId())
                        .token(cpToken)
                        .cpAccessKey(cpAccessKey)
                        .cpAccessSecret(cpAccessSecret)
                        .serverTcpHost(appConstants.getTianyuServer().getTcpHost())
                        .serverTcpPort(appConstants.getTianyuServer().getTcpPort())
                        .serverHttpPrefix(appConstants.getTianyuServer().getHttpPrefix())
                        .build()));
            } else {
                log.error("Company[{}] no cpAccessKey, TcpClient init failed", companyAccessChannel.getYkCompanyId());
            }
        }
    }
}
